package com.example.lostandfoundsystem.web.controllers.system;

import com.example.lostandfoundsystem.models.system.Lost;
import com.example.lostandfoundsystem.models.system.User;
import com.example.lostandfoundsystem.services.common.ResultService;
import com.example.lostandfoundsystem.services.common.UserService;
import gaarason.database.appointment.Paginate;
import gaarason.database.contract.eloquent.Builder;
import gaarason.database.contract.eloquent.Record;
import gaarason.database.exception.EntityNotFoundException;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author lucifer
 * @since 2024-08-04-9:54
 */
@RestController
@RequestMapping("/lost/")
public class LostController {
    @Resource
    Lost.Model lostModel;

    // 查询单个失物
    @GetMapping("/item")
    ResultService.Result item(@RequestParam Map<String, String> params) {
        // 返回值
        ResultService.Result result = ResultService.getNormalResult();
        Record<Lost, Long> lost;
        if (Objects.isNull(params.get("id"))) {
            // 失物不存在
            result.code = ResultService.NOT_FOUND;
            result.msg = "失物不存在";
            return result;
        } else {
            try {
                lost = lostModel.newQuery().findOrFail(params.get("id"));
            } catch (EntityNotFoundException e) {
                // 找不到失物
                result.code = ResultService.NOT_FOUND;
                result.msg = "失物不存在";
                return result;
            }
            // 当前用户
            User user = UserService.getThreadInstance().getUser();
            if (lost.getEntity().getNumber().equals(user.getNumber())) {
                //或者创建者就是当前的管理员，那么可以正常执行
            } else {
                // 否则假装找不到活动
                result.code = ResultService.NOT_FOUND;
                result.msg = "失物不存在";
                return result;
            }
            // 正常返回结果
            result.data = lost.getEntity();
        }
        return result;
    }

    // 查询所有失物
    ResultService.Result index(@RequestParam Map<String, String> params) {
        // 分页单独处理
        String page = params.get("page");
        if (Objects.isNull(page)) {
            // 如果没有页数，默认为第一页
            page = "1";
        }
        // 生成查询器
        Builder<Lost, Long> builder = lostModel.newQuery();
        // 放入参数
        params.forEach((key, value) -> {
            if (!Objects.isNull(key) && !key.equals("page")) {
                // 要去掉page这个参数，如果还要去掉其他就直接在这里加
                if (!Objects.isNull(value)) {
                    builder.where(key, value);
                }
            }
        });
        // 返回结果
        Paginate<Lost> paginate = builder.paginate(page, 16);
        // 对结果进行处理
        Map<String, Object> data = new HashMap<>();
        data.put("currentPage", paginate.getCurrentPage());
        data.put("lastPage", paginate.getLastPage());
        data.put("total", paginate.getTotal());
        data.put("perPage", paginate.getPerPage());
        data.put("data", paginate.getItemList());
        ResultService.Result result = ResultService.getNormalResult();
        result.data = data;
        return result;
    }
}
